from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from utils.error_handle import ui_exception_record

from utils.logger import logger


class BasePage:
    _BASE_URL = ""

    def __init__(self, driver: WebDriver = None):
        if driver:
            # 复用driver
            self.driver = driver
        else:
            chrome_options = webdriver.ChromeOptions()
            chrome_options.add_argument("--window-size=1920,1080")
            chrome_options.add_argument("--disable-extensions")
            chrome_options.add_argument("--headless")
            chrome_options.add_argument("--disable-gpu")
            chrome_options.add_argument("--disable-software-rasterizer")
            chrome_options.add_argument('--no-sandbox')
            chrome_options.add_argument('--ignore-certificate-errors')
            chrome_options.add_argument('--allow-running-insecure-content')
            chrome_options.add_argument("blink-settings=imagesEnabled=false")
            self.driver = webdriver.Remote(command_executor='https://selenium-node.hogwarts.ceshiren.com', options=chrome_options)
            # self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)
            self.driver.maximize_window()
            self.driver.get(self._BASE_URL)

    @ui_exception_record
    def find(self, by, locator=None) -> WebElement:
        """查找元素"""
        logger.info(f"查找元素：by>{by}, locator>{locator}")
        if locator:
            return self.driver.find_element(by, locator)
        else:
            return self.driver.find_element(*by)

    def finds(self, by, locator=None) -> list:
        """查找元素"""
        logger.info(f"查找元素：by>{by}, locator>{locator}")
        if locator:
            return self.driver.find_elements(by, locator)
        else:
            return self.driver.find_elements(*by)

    def find_and_click(self, by, locator=None):
        """查找到之后完成点击"""
        logger.info(f"查找到之后点击")
        self.find(by, locator).click()

    def find_and_send(self, text, by, locator=None):
        """查找到之后完成输入"""
        logger.info(f"查找到之后完成输入：{text}")
        self.find(by, locator).send_keys(text)

    def clear_and_send(self, text, by, locator=None):
        """清除后完成输入"""
        logger.info(f"查找到之后完成输入：{text}")
        element = self.find(by, locator)
        element.clear()
        element.send_keys(text)

    def quit(self):
        """退出"""
        logger.info("退出 quit()")
        self.driver.quit()

    def wait_for_visible(self, locator):
        """显示等待元素可见"""
        logger.info("显式等待等到元素可见")
        WebDriverWait(self.driver, 10, 2). \
            until(expected_conditions.visibility_of_element_located(locator))

    def get_text(self, by, locator):
        """获取文本内容"""
        logger.info("获取元素的文本信息")
        return self.find(by, locator).text

    def switch_to_next_windows(self):
        """切换到最后一个窗口"""
        window_handles = self.driver.window_handles
        self.driver.switch_to.window(window_handles[-1])

    def move_to_element_action(self, by, locator=None):
        """将鼠标移动到指定元素"""
        action = ActionChains(self.driver)
        logger.info(f"查找元素：by>{by}, locator>{locator}")
        if locator:
            action.move_to_element(self.find(by, locator)).perform()
        else:
            action.move_to_element(self.find(*by)).perform()
